﻿using BCCommon;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TMSWorkflowAutoJob.Redis
{
    public static class RedisExtension
    {
        public static IServiceCollection AddRedis(this IServiceCollection services, Action<RedisOptions> setupAction)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }

            services.AddOptions();
            services.Configure(setupAction);

            return services.AddScoped<IRedisService, RedisService>(p =>
            {
                var options = p.GetService<IOptions<RedisOptions>>();

                return new RedisService(options.Value.EnableRedis, options.Value.RedisConnectionString, options.Value.RedisDatabaseNo);
            });
        }
    }
}
